//
//  AddCardbagVC.swift
//  CardBag
//
//  Created by hl on 2019/5/16.
//  Copyright © 2019 hlznj. All rights reserved.
//

import UIKit

class AddCardbagVC: BaseViewController,UITextFieldDelegate,ScanCodeVCDelegate {
    
    var nameTitLabel = UILabel.init()
    var codeTitLabel = UILabel.init()

    var nameTextField = UITextField.init()
    var codeTextField: UITextField = UITextField.init()

    var commitBtn = UIButton.init()

    var scanBtn: UIButton?

    var codeType: CodeType = .unknown

    var lineView1: UIView?
    var lineView2: UIView?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "添加条形码/二维码"

        self.addSubviews()

        self.layout()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.nameTextField.becomeFirstResponder()
    }

    func addSubviews() {
        self.nameTitLabel.text = "商家名称"
        self.codeTitLabel.text = "会员号"

        self.nameTextField.placeholder = "商家名称"
        self.nameTextField.delegate = self as UITextFieldDelegate
        self.nameTextField.addTarget(self, action: #selector(textFieldChanged), for: .editingChanged)

        self.codeTextField.placeholder = "会员号"
        self.codeTextField.delegate = self as UITextFieldDelegate
        self.codeTextField.addTarget(self, action: #selector(textFieldChanged), for: .editingChanged)
        self.codeTextField.keyboardType = .asciiCapable
        self.commitBtn .setTitle("保存", for: .normal)
        self.commitBtn.setTitleColor(UIColor.init(hexString: "#FF3014"), for: .normal)
        self.commitBtn.layer.masksToBounds = true
        self.commitBtn.layer.cornerRadius = 4
        self.commitBtn.layer.borderColor = UIColor.init(hexString: "#FF3014")!.cgColor
        self.commitBtn.layer.borderWidth = 1
        self.commitBtn .addTarget(self, action: #selector(commitBtnAction), for: .touchUpInside)

        self.scanBtn = UIButton.init();
        self.scanBtn?.setImage(UIImage.init(named: "scan"), for: .normal)
        self.scanBtn?.backgroundColor = MainColor1
        self.scanBtn?.layer.masksToBounds = true
        self.scanBtn?.layer.cornerRadius = 4;
        self.scanBtn?.setTitleColor(.black, for: .normal)
        self.scanBtn?.addTarget(self, action: #selector(scanBtnAction), for: .touchUpInside)

        self.lineView1 = UIView.init()
        self.lineView1?.backgroundColor = UIColor.init(hexString: "#EEEEEE")

        self.lineView2 = UIView.init()
        self.lineView2?.backgroundColor = UIColor.init(hexString: "#EEEEEE")

        self.view.addSubview(self.nameTitLabel)
        self.view.addSubview(self.codeTitLabel)
        self.view.addSubview(self.nameTextField)
        self.view.addSubview(self.codeTextField)
        self.view.addSubview(self.commitBtn)
        self.view.addSubview(self.scanBtn!)
        self.view.addSubview(self.lineView1!)
        self.view.addSubview(self.lineView2!)
    }

    //ScanCodeVCDelegate
    func getCodeString(code: String, codeType: CodeType) {
        self.codeTextField.text = code
        self.codeType = codeType
    }

    @objc func scanBtnAction() {
        let vc = ScanCodeVC.init()
        vc.codeDelegate = self as ScanCodeVCDelegate
        self.present(vc, animated: true, completion: nil)
    }

    @objc func commitBtnAction() {
        if self.nameTextField.text!.count <= 0 || self.codeTextField.text!.count <= 0 {
            return
        }
        let cardBag = CardbagModel.init()
        cardBag.cardbagName = self.nameTextField.text!
        cardBag.cardbagCode = self.codeTextField.text!
        switch self.codeType {
        case .Qr:
            cardBag.cardbagType = "Qr"
        case .bar:
            cardBag.cardbagType = "bar"
        default:
            cardBag.cardbagType = "unknown"
        }
        CardbayManager.insertGardbagToMainGroup(cardbag: cardBag)
        self.navigationController?.popViewController(animated: true)
    }

    @objc func textFieldChanged(textField: UITextField) {

        self.codeType = .unknown

        if textField == self.nameTextField {
            if textField.text!.count > 0 {
                self.nameTitLabel.text = "商家名称"
                self.nameTitLabel.textColor = UIColor.black
            } else {
                self.nameTitLabel.text = "需填写店名"
                self.nameTitLabel.textColor = UIColor.init(hexString: "#FF3014")!
            }
        } else if textField == self.codeTextField {
            if textField.text!.count > 0 {
                self.codeTitLabel.text = "会员号"
                self.codeTitLabel.textColor = UIColor.black
            } else {
                self.codeTitLabel.text = "会员卡号不能为空"
                self.codeTitLabel.textColor = UIColor.init(hexString: "#FF3014")!
            }
        }

        //如果有为空的,就红色
        if self.nameTextField.text?.count == 0 || self.codeTextField.text?.count == 0 {
            self.commitBtn.layer.borderColor = UIColor.init(hexString: "#FF3014")!.cgColor
            self.commitBtn.setTitleColor(UIColor.init(hexString: "#FF3014"), for: .normal)
        } else {
            self.commitBtn.layer.borderColor = MainColor1!.cgColor
            self.commitBtn.setTitleColor(MainColor1, for: .normal)
        }
    }

    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        return true
    }

    func layout() {
        self.nameTitLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.nameTextField.snp_top).offset(-4)
            make.left.equalTo(15)
        }
        self.nameTextField.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(kSafeAreaTopHeight+50)
            make.height.equalTo((35))
        }

        self.codeTitLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.codeTextField.snp_top).offset(-4)
            make.left.equalTo(15)
        }
        self.codeTextField.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.right.equalTo(-75)
            make.top.equalTo(self.nameTextField.snp_bottom).offset(50)
            make.height.equalTo((35))
        }
        self.scanBtn?.snp.makeConstraints({ (make) in
            make.right.equalTo(-15)
            make.size.equalTo(CGSize.init(width: 35, height: 35))
            make.bottom.equalTo(self.codeTextField.snp_bottom).offset(-4)
        })

        self.commitBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.init(width: kScreenW/3*2, height: 35))
            make.top.equalTo(self.codeTextField.snp_bottom).offset(40)
            make.centerX.equalTo(self.view.snp_centerX)
        }

        self.lineView1?.snp.makeConstraints({ (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(self.nameTextField.snp_bottom)
            make.height.equalTo(1)
        })
        self.lineView2?.snp.makeConstraints({ (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(self.codeTextField.snp_bottom)
            make.height.equalTo(1)
        })
    }

}
